//
//  LoginViewController.m
//  EaseMob
//
//  Created by wwj on 16/5/26.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "LoginViewController.h"
#import "FWTextField.h"
#import "TBSLoginTool.h"
#import "TabBarViewController.h"
#import <EMSDK.h>
#import <SVProgressHUD.h>

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet FWTextField *nameTF;
@property (weak, nonatomic) IBOutlet FWTextField *pwdTF;

@property (weak, nonatomic) IBOutlet FWTextField *rnameTF;
@property (weak, nonatomic) IBOutlet FWTextField *rpwdTF;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadMargin;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD setMinimumDismissTimeInterval:1];
}

- (IBAction)onClickLogin {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        EMError *error = [[EMClient sharedClient] loginWithUsername:self.nameTF.text password:_pwdTF.text];
        if (!error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //自动登录属性默认是关闭的，需要您在登录成功后设置
                [[EMClient sharedClient].options setIsAutoLogin:YES];
                [UIApplication sharedApplication].keyWindow.rootViewController = [[TabBarViewController alloc] init];
                [SVProgressHUD showSuccessWithStatus:@"登陆成功"];
            });
        }
    });
    
}

- (IBAction)onClickRegist {
    [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
        EMError *error = [[EMClient sharedClient] registerWithUsername:self.rnameTF.text password:self.rpwdTF.text];
        if (!error) {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [UIApplication sharedApplication].keyWindow.rootViewController = [[TabBarViewController alloc] init];
                [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            }];
        }
    }];
    
}


- (IBAction)onClickRegister:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.leadMargin.constant == 0) {
        self.leadMargin.constant = -self.view.width;
    }else {
        self.leadMargin.constant = 0;
    }
    
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    }];
}

@end
